/**
 * 
 */
package org.rollinitiative.d20web.gmportal.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;

/**
 * @author bebopjmm
 * 
 */
public class EncounterExecutionView extends Composite implements CombatantListener
{
    private final Grid encounterStatGrid;

    private final Grid combatantOrderGrid;

    private final TextBox encounterNameBox;

    private final TextBox roundCountBox;

    private final Button controlButton;

    private EncounterServiceAsync encounterService;

    private String encounterID;

    private int rows = 1;


    public EncounterExecutionView()
    {
        VerticalPanel panel = new VerticalPanel();
        encounterNameBox = new TextBox();
        roundCountBox = new TextBox();

        controlButton = new Button("Activate");

        encounterStatGrid = new Grid(1, 5);
        encounterStatGrid.setCellSpacing(0);
        encounterStatGrid.setCellPadding(0);
        encounterStatGrid.setWidth("100%");
        encounterStatGrid.setBorderWidth(1);
        panel.add(encounterStatGrid);

        combatantOrderGrid = new Grid(1, 4);
        combatantOrderGrid.setCellSpacing(0);
        combatantOrderGrid.setCellPadding(0);
        combatantOrderGrid.setWidth("100%");
        combatantOrderGrid.setBorderWidth(1);
        panel.add(combatantOrderGrid);

        initWidget(panel);

        initView();
    }


    public void setEncounterService(EncounterServiceAsync service)
    {
        this.encounterService = service;
    }


    public void setEncounterID(String encounterID)
    {
        this.encounterID = encounterID;
    }


    /*
     * (non-Javadoc)
     * 
     * @see
     * org.rollinitiative.d20web.gmportal.client.CombatantListener#initiateCombatant(java.lang.String
     * )
     */
    public void combatantAware(CombatantData combatant)
    {
        GWT.log("Initiating for encounter execution, combatant: " + combatant.getCombatantName(),
                null);
        encounterService.combatantAware(encounterID, combatant, initiateCombatantCallback);
    }

    AsyncCallback<CombatantData[]> initiateCombatantCallback = new AsyncCallback<CombatantData[]>() {

        /*
         * (non-Javadoc)
         * 
         * @see com.google.gwt.user.client.rpc.AsyncCallback#onFailure(java.lang.Throwable)
         */
        public void onFailure(Throwable caught)
        {
            GWT.log("Call for initiating combatant failed", caught);

        }


        /*
         * (non-Javadoc)
         * 
         * @see com.google.gwt.user.client.rpc.AsyncCallback#onSuccess(java.lang.Object)
         */
        public void onSuccess(CombatantData[] result)
        {
            GWT.log("Updating view for initiated combatants. ", null);
            combatantOrderGrid.resizeRows(++rows);
            combatantOrderGrid.resizeRows(result.length + 1);
            for (int i = 0; i < result.length; i++) {
                CombatantData combatantData = result[i];
                DisclosurePanel initDisclosure = new DisclosurePanel(""
                        + combatantData.getInitiativeVal() + " ("
                        + combatantData.getInitiativeMod() + ")");
                initDisclosure.setContent(new Label("Base + Mods = "
                        + combatantData.getInitiativeBase() + " + "
                        + combatantData.getInitiativeMod()));
                combatantOrderGrid.setWidget(i + 1, 0, initDisclosure);
                combatantOrderGrid.setText(i + 1, 1, combatantData.getCombatantName());
                combatantOrderGrid.setText(i + 1, 2, "" + combatantData.getHitPoints());
                combatantOrderGrid.setText(i + 1, 3, "");
            }

        }

    };


    private void initView()
    {
        encounterNameBox.setText("Default Encounter");
        encounterNameBox.setReadOnly(true);
        roundCountBox.setText("0");
        roundCountBox.setReadOnly(true);

        encounterStatGrid.setText(0, 0, "Encounter Name");
        encounterStatGrid.setWidget(0, 1, encounterNameBox);
        encounterStatGrid.setText(0, 2, "Round");
        encounterStatGrid.setWidget(0, 3, roundCountBox);
        encounterStatGrid.setWidget(0, 4, controlButton);
        encounterStatGrid.getRowFormatter().setStyleName(0, "executionTable-Stats");
        CellFormatter formatter = encounterStatGrid.getCellFormatter();
        formatter.setWidth(0, 0, "20%");
        formatter.setWidth(0, 1, "30%");
        formatter.setWidth(0, 2, "20%");
        formatter.setWidth(0, 3, "10%");
        formatter.setWidth(0, 4, "20%");
        formatter.setHorizontalAlignment(0, 4, HasHorizontalAlignment.ALIGN_CENTER);

        combatantOrderGrid.setText(0, 0, "Initiative");
        combatantOrderGrid.setText(0, 1, "Combatant");
        combatantOrderGrid.setText(0, 2, "Hit Points");
        combatantOrderGrid.setText(0, 3, "Notes");
        combatantOrderGrid.getRowFormatter().setStyleName(0, "executionTable-Header");
        formatter = combatantOrderGrid.getCellFormatter();
        formatter.setWidth(0, 0, "10%");
        formatter.setWidth(0, 1, "25%");
        formatter.setWidth(0, 2, "10%");
        formatter.setWidth(0, 3, "55%");
    }
}
